<?php 
echo CHtml::beginForm('','post',array('enctype'=>'multipart/form-data','class' => 'form')); ?>

<fieldset class="grey inline">
	<legend><?php echo Controller::getActionName($this->action->id); ?></legend>
	
	<div class="notification tip" style="display: block; "> 
    <span class="strong">Tip:</span> <?php echo CHtml::errorSummary(array($model,$profile)); ?>
    </div>
    

	<label>
		<?php echo CHtml::activeLabelEx($model,'username'); ?>
		<?php echo CHtml::activeTextField($model,'username',array('size'=>20,'maxlength'=>20)); ?>
		<?php //echo CHtml::error($model,'username'); ?>
	</label>
    <label>
		<?php echo CHtml::activeLabelEx($model,'password'); ?>
		<?php echo CHtml::activePasswordField($model,'password',array('size'=>60,'maxlength'=>128)); ?>
		<?php //echo CHtml::error($model,'password'); ?>
	</label>
	<label>
		<?php echo CHtml::activeLabelEx($model,'surname'); ?>
		<?php echo CHtml::activeTextField($model,'surname',array('size'=>20,'maxlength'=>20)); ?>
		<?php //echo CHtml::error($model,'username'); ?>
	</label>
		<label>
		<?php echo CHtml::activeLabelEx($model,'telephone'); ?>
		<?php echo CHtml::activeTextField($model,'telephone',array('size'=>20,'maxlength'=>20)); ?>
		<?php //echo CHtml::error($model,'username'); ?>
	</label>
		
	<label>
		<?php echo CHtml::activeLabelEx($model,'email'); ?>
		<?php echo CHtml::activeTextField($model,'email',array('size'=>60,'maxlength'=>128)); ?>
		<?php //echo CHtml::error($model,'email'); ?>
	</label>
    <?php echo CHtml::activeHiddenField($model,'unit_level'); ?>
    <?php echo CHtml::activeHiddenField($model,'unit_id'); ?>
    <br/>
    
    <?php if($model->unit_level != NULL){ ?>
    <label>
		<?php 
		echo CHtml::activeLabelEx($model,'unit_id');		
		switch($model->unit_level){
			case 0:
				echo '<label>超级管理员</label>';
				break; 
			case 1:
				echo '<label>'.MtsysProvince::model()->findByPk($model->unit_id)->PROVINCE_NAME.'</label>';
				break;
			case 2:
				$city =  MtsysCity::model()->findByPk($model->unit_id);
				$sheng = MtsysProvince::model()->find('PROVINCE_ID=:PID',array(
					':PID' => $city->PROVINCE_ID
));
				echo '<label>'.$sheng->PROVINCE_NAME.'&nbsp;&nbsp;-&nbsp;&nbsp;'.$city->CITY_NAME.'</label>';
				break;
			case 3:
				$county =  MtsysCounty::model()->findByPk($model->unit_id);
				$city = MtsysCity::model()->find('CITY_ID=:CID',array(
					':CID' => $county->CITY_ID
));
				$sheng = MtsysProvince::model()->find('PROVINCE_ID=:PID',array(
					':PID' => $city->PROVINCE_ID
));
				echo '<label>'.$sheng->PROVINCE_NAME.'&nbsp;&nbsp;-&nbsp;&nbsp;'.$city->CITY_NAME.'&nbsp;&nbsp;-&nbsp;&nbsp;'.$county->COUNTY_NAME.'</label>';
				break;
			case 4:
				$base = MtsysBase::model()->findByPk($model->unit_id);
			    $county =  MtsysCounty::model()->findByPk($base->COUNTY_ID);
				$city = MtsysCity::model()->find('CITY_ID=:CID',array(
					':CID' => $base->CITY_ID
));
				$sheng = MtsysProvince::model()->find('PROVINCE_ID=:PID',array(
					':PID' => $base->PROVINCE_ID
));
				$unit = User::model()->getUnit($model->id);
				echo '<label>'.$unit.'</label>';
//				echo '<label>'.$sheng->PROVINCE_NAME.'&nbsp;&nbsp;-&nbsp;&nbsp;'.$city->CITY_NAME.'&nbsp;&nbsp;-&nbsp;&nbsp;'.$county->COUNTY_NAME.'&nbsp;&nbsp;-&nbsp;&nbsp;'.$base->BASE_NAME.'</label>';
				break;
		}
		?>
	</label>
    <br/>
    <?php } ?>

	
     <?php 
	/*if(Yii::app()->getModule('user')->isAdmin()){
		include_once('_add_all.php'); 
	}else{
		include_once('_other.php');	
	}	*/
	?>
	
	
    <label>
    <label>角色</label>
    <?php
	$rolesArr = CHtml::listData(AuthItem::model()->findAll('type='.CAuthItem::TYPE_ROLE),'name','description');
	
	$sql = 'select * from authassignment where userid='.$model->id;
	$cmd = Yii::app()->db->createCommand($sql);
	$roles = $cmd->queryAll();
	?>
    <select name="Authitem[name]" id="Authitem_name">
    <?php foreach($rolesArr as $name => $role){?>
    <option <?php echo ($name==$roles[0]['itemname'])?'selected="selected"':'';?> value="<?php echo $name;?>"><?php echo $role;?></option>
    <?php } ?>
    </select>
    </label>
    <input type="hidden" name="User[superuser]" value="0"/> 
    <input type="hidden" name="User[status]" value="1"/>   
	<br/>
	<label>
		<?php //echo CHtml::activeLabelEx($model,'status'); ?>
		<?php 
		//$model->status = 1;
		//echo CHtml::activeDropDownList($model,'status',User::itemAlias('UserStatus')); ?>
		<?php //echo CHtml::error($model,'status'); ?>
	</label>
    <br/>
<?php 
		$profileFields=$profile->getFields();
		if ($profileFields) {
			foreach($profileFields as $field) {
			?>
	<label>
		<?php echo CHtml::activeLabelEx($profile,$field->varname); ?>
		<?php 
		if ($field->widgetEdit($profile)) {
			echo $field->widgetEdit($profile);
		} elseif ($field->range) {
			echo CHtml::activeDropDownList($profile,$field->varname,Profile::range($field->range));
		} elseif ($field->field_type=="TEXT") {
			echo CHtml::activeTextArea($profile,$field->varname,array('rows'=>6, 'cols'=>50));
		} else {
			echo CHtml::activeTextField($profile,$field->varname,array('size'=>60,'maxlength'=>(($field->field_size)?$field->field_size:255)));
		}
		 ?>
		<?php //echo CHtml::error($profile,$field->varname); ?>
	</label>	
			<?php
			}
		}
?>
	<?php echo CHtml::submitButton($model->isNewRecord ? '确定' : '保存',array('class'=>'button blue')); ?>
</fieldset>
<?php echo CHtml::endForm(); ?>
<script>
var UnitSelector = {
	showUnit : function(ele){
		if($(ele).val() == 1){
			$("#select_unit_level").hide();	
			$("#select_unit_id").hide();
		}else{
			$("#select_unit_level").show();
		}	
	},
	'Level' : function(){
		var self = this,
			links = $('input[name="User[unit_level]"]');
		
		links.bind('click',function(){
			$("#select_unit_id").show();
			self.Unit(this.value);
		});
		
		self.Unit(links.filter(":checked").val());
	},
	'Unit' : function(type){
		switch(type){
			case '1':
				$("#PROVINCE_ID").show();
				$("#CITY_ID").hide();
				$("#COUNTY_ID").hide();
				$("#User_BASE_ID").hide();
				break;
			case '2':
				$("#PROVINCE_ID").show();
				$("#CITY_ID").show();
				$("#COUNTY_ID").hide();
				$("#User_BASE_ID").hide();					
				break;
			case '3':
				$("#PROVINCE_ID").show();
				$("#CITY_ID").show();
				$("#COUNTY_ID").show();
				$("#User_BASE_ID").hide();
				break;
			case '4':
				$("#PROVINCE_ID").show();
				$("#CITY_ID").show();
				$("#COUNTY_ID").show();
				$("#User_BASE_ID").show();
				break;
		}
	},
	'init' : function(){
		this.Level();	
	}
};
UnitSelector.init();
</script>   
    
    
